﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using hachiModel;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        try
        {
            regUserName.Value = regUserName.Value.Trim();
                MembershipUser newUser = Membership.CreateUser(regUserName.Value, regPass.Value, regEmail.Value);
                newUser.IsApproved = true;
                Roles.AddUserToRole(regUserName.Value, "member");
                Membership.UpdateUser(newUser);

            CS_Account acc = new CS_Account();
            acc.UserName = regUserName.Value;
            acc.Email = regEmail.Value;
            acc.FirstName = regFirstName.Value.Trim();
            acc.LastName = regLastName.Value.Trim();
            acc.DisplayName = regFirstName.Value.Trim() + " " + regLastName.Value.Trim();
            acc.MobiPhone = regMobi.Value;
            acc.Telephone = regPhone.Value;
            acc.Sex = rdo1.Checked ? 1 : 0;
            acc.Address = regAddress.Value;
            acc.CreatedDate = DateTime.Now;
            acc.Type = 1;
            acc.IsLock = false;
            acc.Birthday = new DateTime(int.Parse(regYear.Value), int.Parse(regMonth.Value), int.Parse(regDay.Value));

            Hachi.Controllers.AccountController actr = new Hachi.Controllers.AccountController();
            if (actr.addAccount(acc))
            {
                FormsAuthentication.SetAuthCookie(regUserName.Value, true);
                Response.Redirect("default.aspx");
            }
            else
                lblMessage.InnerHtml = "Add to entity error!";
            
        }
        catch (MembershipCreateUserException exx)
        {
            lblMessage.InnerHtml = GetErrorMessage(exx.StatusCode);
        }
        catch (HttpException ex)
        {
            lblMessage.InnerHtml = ex.Message;
        }
    }
    public string GetErrorMessage(MembershipCreateStatus status)
    {
        switch (status)
        {
            case MembershipCreateStatus.DuplicateUserName:
                return "<script>alert(lang_regUserNameInUse);</script>";

            case MembershipCreateStatus.DuplicateEmail:
                return "<script>alert(lang_regUserEmailInUse);</script>";

            case MembershipCreateStatus.InvalidPassword:
                return "The password provided is invalid. Please enter a valid password value.";

            case MembershipCreateStatus.InvalidEmail:
                return "The e-mail address provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidAnswer:
                return "The password retrieval answer provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidQuestion:
                return "The password retrieval question provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidUserName:
                return "The user name provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.ProviderError:
                return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

            case MembershipCreateStatus.UserRejected:
                return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

            default:
                return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
        }
    }
}